import AbstractRuntimeView from "../../../common/abstract/mvc/AbstractRuntimeView";
import Event from "../../../common/manager/EventClick";
import ArrayUtils from "../../../common/util/ArrayUtils";
import MyTools from "../../../common/util/MyTools";
import { ADMgr } from "../../../common/wxComponent/ADManager";

/**
 * 
 * @author: xj.huang
 * @date: 2021/08/02 10:18:11
 */
export default class HotView extends AbstractRuntimeView<null, null> {
    private ReGame: Laya.Image;
    private bg: Laya.Image;
    private pageInfo: Game.Box;
    private ViewList: Laya.List;
    public param;
    private topBox:Laya.Box
    constructor() {
        super()
    }
    onOnce(param) {
        console.log(this.param,"this.param.pageInfo")
        this.pageInfo = this.gameConfig.ad_move_conf.page.hot_box
        MyTools.ChangeBGColor(this.bg, this.pageInfo.color);
        this.upListData(this.gameConfig.game_box)
        this.addEvent()
    }
    private addEvent() {
        this.ViewList.renderHandler = new Laya.Handler(this, this.onUpItem);
        this.ViewList.vScrollBarSkin = "";
        Event.onClick(this.ReGame, this, this.onCloesView);
    }
    onEnable() {
        super.onEnable();
        this.param.pageInfo.currIndex++
        ADMgr.wxAdRipoff.isShowBtn(this.ReGame, this.pageInfo, MyTools.getRandomPer(this.param.pageInfo.is_banner));
        // console.log("this.param",this.param,this.pageInfo)
    }
    private upListData(data:any[] = []) {
        let _data = ArrayUtils.conleData(data) as any[];
        for(let k in _data){
            if(_data[k]['isHot']==undefined)_data[k]['isHot'] = this.getIsHot(_data[k]['is_hot'])
        }
        let listData = _data.slice(3,data.length);
        this.ViewList.array = listData;
        this.ViewList.refresh();
        this.topBan(_data.slice(0,3))
    }
    /**渲染列表数据 */
    private onUpItem(cell: Laya.Box, index: number) {
        let IDEIndex = cell.getChildByName("index") as Laya.Label;
        let lblName = cell.getChildByName("lblName") as Laya.Label;
        let playNum = cell.getChildByName("playNum") as Laya.Label;
        let gameicon = cell.getChildByName("gameicon") as Laya.Image;
        let hotOrNew = cell.getChildByName("hot_or_new") as Laya.Image;
        let play = cell.getChildByName("play") as Laya.Image;
        let hand = cell.getChildByName("hand") as Laya.Image;
        cell["appId"] = cell.dataSource.app_id;
        gameicon.skin = cell.dataSource.icon_url;
        IDEIndex.text = (index+4) + "";
        lblName.text = cell.dataSource.name
        playNum.text = MyTools.GetPlayNum(index+4, this.ViewList.array.length);
        hotOrNew.visible = cell.dataSource.isHot
        hotOrNew.skin = `ZhiYingRes/DaoChu/texture/Hot_hot${MyTools.getRandomNumInt(1, 3)}.png`;
        hand.visible = cell.dataSource.isHot
        Event.offClick(cell, this, this.onToApp)
        Event.onClick(cell, this, this.onToApp, [cell.dataSource])
    }
    private getIsHot(v) {
        if (v!= 0) {
            if (Math.random() <= this.gameConfig.box_conf.hand_and_icon.hand / 100) {
                return true
            }
            return false
        }
        return false
    }
    private onToApp(dataSource) {
        ADMgr.openWxAPP(dataSource.app_id,dataSource.path,dataSource)
    }
    private topBan(dataSource){
        for(var _k in this.topBox['_children']){
            var k = Number(_k);
            let box = this.topBox['_children'][k] as Laya.Box;
            box.dataSource = dataSource[k];
            let gameicon = box.getChildByName("gameicon") as Laya.Image;
            let bg = box.getChildByName("bg") as Laya.Image;
            let lblName = box.getChildByName("lblName") as Laya.Label;
            let playNum = box.getChildByName("playNum") as Laya.Label;
            gameicon.skin = box.dataSource.icon_url
            lblName.text = box.dataSource.name
            playNum.text = MyTools.GetPlayNum(k, this.ViewList.array.length);
            Event.onClick(box, this, this.onToApp, [box.dataSource])
        }
    }
    onCloesView() {
        this.onClose("手动关闭")
        this.asyncExportOpen(this.param.pageInfo, this.param.viewType,this.param.endCallBack)
    }

}